require "Cocos2d"
require "src/scene/SpriteFrameMgr"

SceneMgr = {}
SceneType = {
  SCENE_INVALID = 0,
  SCENE_LOADING = 1,
  SCENE_LOGIN = 2,
  SCENE_CENTER = 3,
  SCENE_ROOM = 4,
  SCENE_GAME = 5
}
SceneMgr.m_currentScene = SceneType.SCENE_INVALID
SceneMgr.m_gameFromScreen = SceneType.SCENE_CENTER
SceneMgr.m_fromScreen = SceneType.SCENE_INVALID
SceneMgr.gameScene = nil
function SceneMgr:getCurrentScene()
  return self.m_currentScene
end
function SceneMgr:switchToLogin()
  self.m_currentScene = SceneType.SCENE_LOGIN
  cc.Director:getInstance():getOpenGLView():setIMEKeyboardState(false)
  local scene = require("src/scene/LoginScene")
  self.gameScene = scene:create()
  if cc.Director:getInstance():getRunningScene() then
    cc.Director:getInstance():replaceScene(self.gameScene)
  else
    cc.Director:getInstance():runWithScene(self.gameScene)
  end
end
function SceneMgr:switchToLoading()
  self.m_currentScene = SceneType.SCENE_LOADING
  local scene = require("scene/LoadingScene")
  self.gameScene = scene:create()
  if cc.Director:getInstance():getRunningScene() then
    cc.Director:getInstance():replaceScene(self.gameScene)
  else
    cc.Director:getInstance():runWithScene(self.gameScene)
  end
end
function SceneMgr:switchToRoom()
  SpriteFrameMgr:getInstance():cleanupPurgeCachedResource()
  cc.Director:getInstance():getOpenGLView():setIMEKeyboardState(false)
  if self.m_currentScene ~= SceneType.SCENE_ROOM and self.m_currentScene ~= SceneType.SCENE_LOGIN then
    cc.SimpleAudioEngine:getInstance():stopMusic()
  else
  end
  self.m_currentScene = SceneType.SCENE_ROOM
  self.m_gameFromScreen = SceneType.SCENE_ROOM
  self.m_fromScreen = SceneType.SCENE_ROOM
  self.gameScene = require("scene/RoomScene"):create()
  if cc.Director:getInstance():getRunningScene() then
    cc.Director:getInstance():replaceScene(self.gameScene)
  else
    cc.Director:getInstance():runWithScene(self.gameScene)
  end
end
function SceneMgr:switchToMyCenter()
  print("========switchToMyCenter===========")
  self.m_currentScene = SceneType.SCENE_CENTER
  self.m_gameFromScreen = SceneType.SCENE_CENTER
  SpriteFrameMgr:getInstance():cleanupPurgeCachedResource()
  cc.Director:getInstance():getOpenGLView():setIMEKeyboardState(false)
  local scene = require("src/scene/MyCenterScene")
  self.gameScene = scene:create()
  if cc.Director:getInstance():getRunningScene() then
    cc.Director:getInstance():replaceScene(self.gameScene)
  else
    cc.Director:getInstance():runWithScene(self.gameScene)
  end
end
function SceneMgr:enterGame(cleanupResource)
  self.m_currentScene = SceneType.SCENE_GAME
  self.m_fromScreen = SceneType.SCENE_GAME
  cc.Director:getInstance():getOpenGLView():setIMEKeyboardState(false)
  self.gameScene = require("src/scene/GameScene"):create()
  if cc.Director:getInstance():getRunningScene() then
    cc.Director:getInstance():replaceScene(self.gameScene)
  else
    cc.Director:getInstance():runWithScene(self.gameScene)
  end
end
